/* EIX - IPC system
 * Copyright (C) 2008-2011 Jorge Luis Zapata
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */
#include "Eix.h"
#include "eix_private.h"

#include <unistd.h>
#include <fcntl.h>

#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif

#ifdef HAVE_SYS_UN_H
# include <sys/un.h>
#endif

#ifdef _WIN32
# include <winsock2.h>
#endif
/*============================================================================*
 *                                  Local                                     *
 *============================================================================*/
/*============================================================================*
 *                                 Global                                     *
 *============================================================================*/
Eina_Bool eix_transport_posix_wait_write(int fd)
{
	fd_set writeset;

	/* wait until we can send something */
	FD_ZERO(&writeset);
	FD_SET(fd, &writeset);

	/* TODO use the correct timeout */
	if (select(fd + 1, NULL, &writeset, NULL, NULL) <= 0)
	{
		ERR("select() failed");
		return EINA_FALSE;
	}
	if (!FD_ISSET(fd, &writeset))
	{
		ERR("our fd is not on the fd set");
		return EINA_FALSE;
	}
	return EINA_TRUE;
}

Eina_Bool eix_transport_posix_wait_read(int fd)
{
	fd_set readset;

	/* wait until we can receive something */
	FD_ZERO(&readset);
	FD_SET(fd, &readset);

	/* TODO use the correct timeout */
	if (select(fd + 1, &readset, NULL, NULL, NULL) <= 0)
	{
		ERR("select() failed");
		return EINA_FALSE;
	}
	if (!FD_ISSET(fd, &readset))
	{
		ERR("our fd is not on the fd set");
		return EINA_FALSE;
	}
	return EINA_TRUE;
}
/*============================================================================*
 *                                   API                                      *
 *============================================================================*/

